# -*- coding: utf-8 -*-
# @Author    : Tian Wencheng
# @CreateTime: 2021/1/9 19:22
# @FileName  : login.py.py
# @IDE       : PyCharm
# @Git       : https://gitee.com/dianetian/projects


import json
import hashlib
from common.request import HTTPRequest
from interface.base import BaseModule


class LoginModule(BaseModule):
    def __init__(self):
        self.request_url = "/api/login"
        self.request_method = "POST"
        self.request_headers = {"Content-Type": "application/json"}
        self.request_params = {"name": "", "password": ""}
        self.cookies = super().cookies
        self.web_url = super().web_url
        self.url = self.web_url + self.request_url

    def user_login(self, **kwargs):
        name = kwargs.get("name")
        password = kwargs.get("password")
        hashlib_instance = hashlib.md5()
        tobe_encrypt_string = name + password
        hashlib_instance.update(tobe_encrypt_string.encode("utf-8"))
        encrypt_password = hashlib_instance.hexdigest()
        self.request_params["name"] = name
        self.request_params["password"] = encrypt_password

        response = HTTPRequest().http_request(method=self.request_method, url=self.url, headers=self.request_headers,
                                              params=self.request_params)
        if json.loads(response.get("data").get("text")).get("data").get("successful") is True:
            BaseModule.cookies = response.get("data").get("cookies")
        return response


if __name__ == "__main__":
    # print(LoginModule().user_login("admin", "12345"))
    # print(BaseModule.cookies)
    print(LoginModule().user_login(**{'name': 'admin', 'password': '12345'}))
